package com.database.bean;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name="COMPANYROUNDDECISIONANDRESULT")
@AttributeOverride( name="id", column = @Column(name="id") )
@XmlRootElement
public class CompanyRoundDecisionAndResult extends BaseBean{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@ManyToOne
    @JoinColumn(name="roundId")
	private Round round=null;
	
	@ManyToOne
    @JoinColumn(name="companyId")
	private User user=null;
	
	@Column(name="revenue")
	private Double revenue=0.0;
	
	@Column(name="expenses")
	private Double expenses=0.0;
	
	@Column(name="logisticCosts")
	private Double logisticCosts=0.0;
	
	@Column(name="overdraftCosts")
	private Double overdraftCosts=0.0;
	
	@Column(name="marketingInvestment")
	private Double marketingInvestment=0.0;
	
	@Column(name="employerEducationInvestment")
	private Double employerEducationInvestment=0.0;
	
	@Column(name="rdInvestment")
	private Double rdInvestment=null;
	
	@Column(name="newImage")
	private Double newImage=null;
	
	@Column(name="newBudget")
	private Double newBudget=null;
	
	@Column(name="reportCosts")
	private Double reportCosts=null;
	
	@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
	@Fetch(value = FetchMode.SUBSELECT)
    @JoinColumn(name="decresId")
	private List<CompanyProduct>companyProducts =new ArrayList<CompanyProduct>();
	
	@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
	@Fetch(value = FetchMode.SUBSELECT)
    @JoinColumn(name="decres_Id")
	private List<ReportDecision>reportDecisions =new ArrayList<ReportDecision>();
	
	//RCIS...Relative Company Image Spending
	@Transient
	private Double rcis = 0.0; 
	
	public CompanyRoundDecisionAndResult() {
		this.employerEducationInvestment = 0.0;
		this.marketingInvestment = 0.0;
		this.rdInvestment = 0.0;
		this.revenue = 0.0;
		this.expenses = 0.0;
		this.logisticCosts = 0.0;
		this.overdraftCosts = 0.0;
		this.newBudget = 0.0;
		this.reportCosts = 0.0;
	}
	
	/**
	 * Create a copy of the CompanyRoundDecisionAndResult for a given new Round
	 * @return
	 */
	public CompanyRoundDecisionAndResult getCopy(Round newRound){
		CompanyRoundDecisionAndResult decres = new CompanyRoundDecisionAndResult();
		decres.setEmployerEducationInvestment(this.employerEducationInvestment);
		decres.setMarketingInvestment(this.marketingInvestment);
		decres.setRdInvestment(this.rdInvestment);
		decres.setUser(this.user);
		decres.setRound(newRound);
		
		CompanyProduct copy=null;
		for(CompanyProduct cp:this.getCompanyProducts()){
			copy = cp.getCopy(decres);
			//this.companyService.update(copy);
			decres.addCompanyProduct(copy);
		}
		
		ReportDecision copyRepDec=null;
		for(ReportDecision repdec:this.getReportDecisions()){
			copyRepDec = repdec.getCopy(decres);
			//this.companyService.update(copyRepDec);
			decres.addReportDecision(copyRepDec);
		}
		
		return decres;
	}

	/**
	 * 
	 * @return the amount of sold Products
	 */
	public int getAmountSoldProducts(){
		int amountSold=0;
		
		for(CompanyProduct cp: this.getCompanyProducts()){
			amountSold=amountSold+cp.getAmountSold();
		}
		
		return amountSold;
	}
	
	/**
	 * @return the round
	 */
	public Round getRound() {
		return round;
	}

	/**
	 * @param round the round to set
	 */
	public void setRound(Round round) {
		this.round = round;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the revenue
	 */
	public Double getRevenue() {
		return revenue;
	}

	/**
	 * @param revenue the revenue to set
	 */
	public void setRevenue(Double revenue) {
		this.revenue = revenue;
	}
	
	/**
	 * Add an additional revenue
	 * @param revenue
	 */
	public void addRevenue(Double revenue) {
		if(this.revenue!=null)
			this.revenue +=revenue;
		else
			this.revenue = revenue;
	}


	/**
	 * @return the marketingInvestment
	 */
	public Double getMarketingInvestment() {
		return marketingInvestment;
	}

	/**
	 * @param marketingInvestment the marketingInvestment to set
	 */
	public void setMarketingInvestment(Double marketingInvestment) {
		this.marketingInvestment = marketingInvestment;
	}

	/**
	 * @return the employerEducationInvestment
	 */
	public Double getEmployerEducationInvestment() {
		return employerEducationInvestment;
	}

	/**
	 * @param employerEducationInvestment the employerEducationInvestment to set
	 */
	public void setEmployerEducationInvestment(Double employerEducationInvestment) {
		this.employerEducationInvestment = employerEducationInvestment;
	}

	/**
	 * @return the rdInvestment
	 */
	public Double getRdInvestment() {
		return rdInvestment;
	}

	/**
	 * @param rdInvestment the rdInvestment to set
	 */
	public void setRdInvestment(Double rdInvestment) {
		this.rdInvestment = rdInvestment;
	}

	/**
	 * @return the companyProducts
	 */
	public List<CompanyProduct> getCompanyProducts() {
		return companyProducts;
	}

	/**
	 * @param companyProducts the companyProducts to set
	 */
	public void setCompanyProducts(List<CompanyProduct> companyProducts) {
		this.companyProducts = companyProducts;
	}

	/**
	 * @return the reportDecisions
	 */
	public List<ReportDecision> getReportDecisions() {
		return reportDecisions;
	}

	/**
	 * @param reportDecisions the reportDecisions to set
	 */
	public void setReportDecisions(List<ReportDecision> reportDecisions) {
		this.reportDecisions = reportDecisions;
	}

	/**
	 * Add a new reportDecision to the already existing reportDecisions.
	 * @param reportDecisions
	 */
	public void addReportDecision(ReportDecision reportDecision){
		if(this.reportDecisions==null){
			this.reportDecisions = new ArrayList<ReportDecision>();
		}
		this.reportDecisions.add(reportDecision);
	}
	
	
	public void addCompanyProduct(CompanyProduct companyProduct){
		if(this.companyProducts==null){
			this.companyProducts = new ArrayList<CompanyProduct>();
		}
		this.companyProducts.add(companyProduct);
	}
	
	public void removeCompanyProduct(CompanyProduct companyProduct){
		if(this.companyProducts!=null){
			this.companyProducts.remove(companyProduct);
		}
	}
	
	/**
	 * Remove a already existing reportDecision from the list
	 * @param reportDecisions
	 */
	public void removeReportDecision(ReportDecision reportDecision){
		if(this.reportDecisions!=null){
			this.reportDecisions.remove(reportDecision);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public Double getExpenses() {
		return expenses;
	}

	/**
	 * 
	 * @param expenses
	 */
	public void setExpenses(Double expenses) {
		this.expenses = expenses;
	}
	
	/**
	 * Add additional expenses
	 * @param expenses
	 */
	public void addExpenses(Double expenses) {
		if(this.expenses!=null)
			this.expenses +=expenses;
		else
			this.expenses = expenses;
	}

	/**
	 * @return the logisticCosts
	 */
	public Double getLogisticCosts() {
		return logisticCosts;
	}

	/**
	 * @param logisticCosts the logisticCosts to set
	 */
	public void setLogisticCosts(Double logisticCosts) {
		this.logisticCosts = logisticCosts;
	}

	/**
	 * @return the overdraftCosts
	 */
	public Double getOverdraftCosts() {
		return overdraftCosts;
	}

	/**
	 * @param overdraftCosts the overdraftCosts to set
	 */
	public void setOverdraftCosts(Double overdraftCosts) {
		this.overdraftCosts = overdraftCosts;
	}

	/**
	 * @return the rcis
	 */
	public Double getRcis() {
		return rcis;
	}

	/**
	 * @param rcis the rcis to set
	 */
	public void setRcis(Double rcis) {
		this.rcis = rcis;
	}

	public Double getNewImage() {
		return newImage;
	}

	public void setNewImage(Double newImage) {
		this.newImage = newImage;
	}

	/**
	 * @return the newBudget
	 */
	public Double getNewBudget() {
		return newBudget;
	}

	/**
	 * @param newBudget the newBudget to set
	 */
	public void setNewBudget(Double newBudget) {
		this.newBudget = newBudget;
	}

	/**
	 * @return the reportCosts
	 */
	public Double getReportCosts() {
		return reportCosts;
	}

	/**
	 * @param reportCosts the reportCosts to set
	 */
	public void setReportCosts(Double reportCosts) {
		this.reportCosts = reportCosts;
	}

	@Override
	public String toString() {
		return "CompanyRoundDecisionAndResult [round=" + round + ", user="
				+ user + ", revenue=" + revenue + ", expenses=" + expenses
				+ ", logisticCosts=" + logisticCosts + ", overdraftCosts="
				+ overdraftCosts + ", marketingInvestment="
				+ marketingInvestment + ", employerEducationInvestment="
				+ employerEducationInvestment + ", rdInvestment="
				+ rdInvestment + ", newImage=" + newImage + ", newBudget="
				+ newBudget + ", reportCosts=" + reportCosts
				+ ", companyProducts=" + companyProducts + ", reportDecisions="
				+ reportDecisions + ", rcis=" + rcis + "]";
	}
}
